<template>
  <div class="edit">
    <el-dialog :visible.sync="dialogVisible" :show-close="false" :before-close="handleClose" :close-on-click-modal="false">
      <div class="dialogContainer">
        <div class="head">
          <span v-if="isEdit">编辑用户</span>
          <span v-else>用户详情</span>
        </div>
        <div class="content" v-show="isEdit === true">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="微信昵称" prop="wechatName">
              <el-input v-model="ruleForm.wechatName" disabled></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-select v-model="ruleForm.sex" placeholder="请选择性别">
                <el-option label="男" value="0"></el-option>
                <el-option label="女" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="电话号码" prop="telephone">
              <el-input v-model="ruleForm.telephone"></el-input>
            </el-form-item>
            <el-form-item label="车牌号">
              <div class="carList">
                <div v-for="item in carId" :key="item.id" class="item">
                  <button class="del" @click="deleteCarId(item.id)" v-if="carId.length > 1">-</button>
                  <input type="text" name="" id="" v-model="item.carId" />
                </div>
                <button class="add" @click="addCarId">添加</button>
              </div>
              <!-- <el-input v-for="item in carId" :key="item.carId" v-model="item.carId" type="text"></el-input> -->
              <!-- <el-input v-model="ruleForm.name"></el-input> -->
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input v-model="ruleForm.address"></el-input>
            </el-form-item>
            <el-form-item label="备注" class="memo" prop="memo">
              <el-input v-model="ruleForm.memo" type="textarea"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div class="content" v-show="isEdit === false">
          <el-form :model="ruleForm2" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名">
              <el-input v-model="ruleForm2.name" readonly></el-input>
            </el-form-item>
            <el-form-item label="微信昵称">
              <el-input v-model="ruleForm2.wechatName" readonly></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-input value="男" readonly v-if="ruleForm2.sex === 0"></el-input>
              <el-input value="男" readonly v-if="ruleForm2.sex === '0'"></el-input>
              <el-input value="女" readonly v-if="ruleForm2.sex === 1"></el-input>
              <el-input value="女" readonly v-if="ruleForm2.sex === '1'"></el-input>
              <!-- <el-select v-model="ruleForm2.sex" placeholder="请选择性别" readonly>
      <el-option label="男" value="0"></el-option>
      <el-option label="女" value="1"></el-option>
    </el-select> -->
            </el-form-item>
            <el-form-item label="电话号码">
              <el-input v-model="ruleForm2.telephone" readonly></el-input>
            </el-form-item>
            <el-form-item label="车牌号">
              <div class="carList">
                <div v-for="item in carId" :key="item.id" class="item">
                  <input type="text" name="" id="" v-model="item.carId" readonly />
                </div>
              </div>
              <!-- <el-input v-for="item in carId" :key="item.carId" v-model="item.carId" type="text"></el-input> -->
              <!-- <el-input v-model="ruleForm.name"></el-input> -->
            </el-form-item>
            <el-form-item label="地址">
              <el-input v-model="ruleForm2.address" readonly></el-input>
            </el-form-item>
            <el-form-item label="备注" class="memo" readonly>
              <el-input v-model="ruleForm2.memo" type="textarea" readonly></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div class="btn" v-if="isEdit">
          <button @click="cancelEdit">取消</button>
          <button @click="submitForm('ruleForm')">确定</button>
        </div>
        <div class="btn" v-else>
          <button @click="cancel">关闭</button>
          <button @click="isEdit = true">编辑</button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      userId: '',
      ruleForm: {
        name: '',
        telephone: '',
        address: '',
        sex: ''
      },
      ruleForm2: {
        name: '',
        telephone: '',
        address: '',
        sex: ''
      },
      carId2: [
        { carId: '', id: 1 }
      ],
      carId: [
        { carId: '', id: 1 }
      ],
      rules: {
        name: [
          { message: '姓名不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'change' }
        ],
        address: [
          { message: '地址不能为空', trigger: 'blur' },
          { min: 0, max: 50, message: '长度 50 个字符内', trigger: 'change' }
        ],
        sex: [
          { message: '请选择性别', trigger: 'change' }
        ],
        telephone: [
          { message: '请输入电话号码', trigger: 'blur' },
          { min: 11, max: 11, message: '长度为11个字符', trigger: 'change' }
        ],
        wechatName: [
          { min: 0, max: 20, message: '长度 20 个字符内', trigger: 'change' }
        ],
        memo: [
          { min: 0, max: 50, message: '长度 50 个字符内', trigger: 'change' }
        ]
      },
      addId: '',
      isEdit: false
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.isEdit = false
        })
        .catch(_ => { })
    },
    cancelEdit() {
      this.getUserInfo(this.userId)
      this.isEdit = false
    },
    // 打开编辑模态框并获取信息
    openDetail(val) {
      console.log(val)
      this.getUserInfo(val)
      this.dialogVisible = true
      this.userId = val
    },
    // 获取用户信息
    async getUserInfo(val) {
      const { data } = await this.$http.get('/user/one', { params: { id: val } })
      console.log(data)
      if (data.code === 200) {
        this.ruleForm = data.data
        this.ruleForm2 = data.data
        // this.carId2
        console.log(this.ruleForm)
        this.ruleForm.sex = this.ruleForm.sex + ''
        if (data.data.carIdList.length > 0) {
          this.carId = data.data.carIdList
          this.carId.forEach((item, index) => {
            item.id = index + 1
          })
          this.addId = this.carId.length + 1
        }
      }
    },
    // 确认修改
    async submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          console.log(this.ruleForm)
          this.carId.forEach(item => {
            delete item.id
            item.userId = Number(sessionStorage.getItem('userId'))
          })
          console.log(this.carId)
          let arr = { user: { ...this.ruleForm } }
          delete arr.user.carIdList
          arr = { ...arr, carList: [...this.carId] }
          delete arr.user.carId
          delete arr.user.createTime
          console.log(arr)
          const res = await this.$http.post('/user', arr)
          console.log(res.data)
          if (res.data.code === 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.$emit('getUserList')
            this.isEdit = false
            this.getUserInfo(this.userId)
            this.dialogVisible = false
          } else {
            this.$message.error(`修改失败，${res.data.msg}`)
          }
        }
      })
      //   this.$delete(arr.user)
    },
    // 取消修改
    cancel() {
      this.carId = [
        { carId: '', id: 1 }
      ]
      this.isEdit = false
      this.dialogVisible = false
    },
    // 增加车牌号
    addCarId() {
      // console.log(this.carId)
      this.carId = [...this.carId, { carId: '', id: this.addId }]
      this.addId++
    },
    // 删除车牌号
    deleteCarId(val) {
      console.log(val)
      if (this.carId.length < 2) {
        this.$message('车牌号不能为空')
      } else {
        const newData = this.carId.filter(item => {
          return item.id !== val
        })
        this.carId = newData
      }
    }
  },
  beforeDestroy() {
    console.log(this.isEdit)
    this.isEdit = false
  }
}
</script>
<style>
body,
html,
#app {
  min-width: 1100px !important;
}
</style>
<style scoped lang="less">
.edit {
  .dialogContainer {
    button {
      cursor: pointer;
    }
    .head {
      width: 100%;
      height: 40px;
      background: #eeeeee;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .content {
      padding: 20px;
      height: 450px;
      .carList {
        display: flex;
        flex-wrap: wrap;
        .add {
          margin-left: 10px;
          border: 0;
          height: 40px;
          min-width: 50px;
          font-size: 14px;
        }
        .item {
          position: relative;
        }
        .del {
          position: absolute;
          right: 0;
          border: 0px;
        }
        input {
          width: 120px;
          height: 40px;
          border: 1px solid #dcdfe6;
          box-sizing: border-box;
          color: #606266;
          display: inline-block;
          font-size: inherit;
          margin-left: 5px;
          margin-bottom: 5px;
          border-radius: 5px;
        }
      }
      .memo {
        /deep/.el-input__inner {
          height: 82px;
          background: #ffffff;
          border: 1px solid #eeeeee;
        }
      }
    }
    .btn {
      padding: 0px 100px;
      height: 76px;
      display: flex;
      justify-content: space-evenly;
      button {
        width: 99px;
        height: 35px;
        background: #ffffff;
        border-radius: 20px;
        border: 1px solid #333333;
        &:nth-child(2) {
          background: #333333;
          color: #ffffff;
        }
      }
    }
  }
}
/deep/.el-dialog__header,
/deep/.el-dialog__body {
  padding: 0;
}
/deep/.el-dialog {
  width: 785px !important;
  box-shadow: 3px 3px 11px 0px #3d3d3d;
}
</style>
